<template>
    <a-card :body-style="{padding: '24px 32px'}" :bordered="false">
        <!--<div style="width:100%; padding-top:10px; padding-left:20px; margin-bottom: 30px" align="left">
            <a-button @click="handleSubmit" type="primary" >生成授权文件</a-button>
        </div>-->
        <a-form :form="form">
            <a-form-item label="单位全称" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                <a-input v-decorator="['orgName', {rules: [{ required: true, message: '请输入单位全称', whitespace: true}]}]"/>
            </a-form-item>

            <a-form-item label="软件产品全称" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                <a-input v-decorator="['softName', {rules: [{ required: true, message: '请输入软件产品全称', whitespace: true}]}]"/>
            </a-form-item>

            <a-form-item label="授权日期（yyyy-MM-dd）" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                <a-date-picker style="width: 100%" placeholder="请输入授权日期" v-decorator="['authDateStr']"/>
            </a-form-item>

            <a-form-item label="截止日期（yyyy-MM-dd）" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                <a-date-picker style="width: 100%" placeholder="请输入截止日期" v-decorator="['stopDateStr']"/>
            </a-form-item>

            <a-form-item label="授权用户最大数量" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                <a-input v-decorator="['userCountStr', {rules: [{ required: true, message: '请输入授权用户最大数量', whitespace: true}]}]"/>
            </a-form-item>

            <a-form-item label="MAC地址" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                <a-input v-decorator="['mac', {rules: [{ required: true, message: '请输入MAC地址', whitespace: true}]}]"/>
            </a-form-item>

            <a-form-item label="IP地址" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                <a-input v-decorator="['ipAddress', {rules: [{ required: true, message: '请输入IP地址', whitespace: true}]}]"/>
            </a-form-item>
        </a-form>
        <div class="drawer-bootom-button" style="width:100%; padding-bottom: 20px;bottom:auto" align="center">
            <a-button @click="handleSubmit" type="primary" >生成授权文件</a-button>
            <!--<a-button style="margin-left: 50px" @click="importSubmit" type="primary" >导入授权文件</a-button>-->
        </div>

        <a-modal v-model="importLic" :destroyOnClose="true" title="导入授权文件" @ok="handleOk">
        <!--<div id="importLicDiv" style="display: none;">-->
                <table style="width:100%; height:105px; text-align:center">
                    <tr>
                        <td class="tCont"><span style="font-weight: bold;">选择授权文件 :</span></td>
                        <td class="tCont">
                            <!--<v-input type="file" name="lic" id="lic" v-decorator="['lic']"/>-->
                            <a-upload
                                    name="file"
                                    :multiple="false"
                                    action="/platform/system/sysLicense/import.ht"
                                    :showUploadList="true"
                                    @change="handleChange"
                            >
                                <a-button> <a-icon type="upload" />上传授权文件</a-button>
                            </a-upload>
                        </td>
                    </tr>
                </table>
        <!--</div>-->
        </a-modal>
    </a-card>

</template>

<script>
    import pick from 'lodash.pick'
    import {saveLicense} from '@/api/system'
    import {message} from 'ant-design-vue';

    export default {
        name: "licenseForm",
        data() {
            return {
                //创建表单
                form:this.$form.createForm(this),
                //实体对象
                license: [],
                //是否展示到菜单
                displayInMenu:true,
                //是否有子节点
                folder:false,
                //是否新打开网页
                newOpen:true,
                //默认打开
                open:true,
                importLic: false
            };
        },
        methods: {
            //日期校验
            isValidateDate(date){
                var result = false;
                var regDate = /^[0-9]{4}-[0-1]?[0-9]{1}-[0-3]?[0-9]{1}$/;
                if(regDate.test(date)){
                    result = true;
                }
                return result;
            },
            //正整数校验
            isValidateNum(num) {
                var result = false;
                var regNum = /(^[1-9]\d*$)/;
                if (regNum.test(num)) {
                    return true;
                }
                return result;
            },
            //mac网络地址校验
            isValidateMac(mac){
                var result = false;
                //var regex = "([A-Fa-f0-9]{2}-){5}[A-Fa-f0-9]{2}";
                var regMac = /(([0-9A-Fa-f]{2}-){5}[0-9A-Fa-f]{2})+/;
                if(regMac.test(mac)){
                    result = true;
                }
                return result;
            },
            //ip地址校验
            isValidateIp(ip){
                var exp = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
                var reg = ip.match(exp);
                if(reg == null){
                    return false;
                }else{
                    return true;
                }
            },
            importSubmit(){
                this.importLic = true;
            },
            handleOk(e) {
                this.importLic = false;
            },
            handleChange(info) {
                if (info.file.status === 'done') {
                    //此处：info.file.response.sysFile 可以打印后台返回的数据fileId: fileName:  filePath: 等
                    console.log('info.file.response.sysFile',info.file.response.sysFile)
                    if (info.file.response.success) {
                        //返回成功
                        this.$message.success(`${info.file.name} 导入成功!`);
                    } else {
                        this.$message.error(`${info.file.response.message}`);
                    }
                } else if (info.file.status === 'error') {
                    this.$message.error(`${info.file.response.message}`);
                }
            },
            handleSubmit(){
                let that = this;
                // 触发表单验证
                this.form.validateFieldsAndScroll((err, values) => {
                    if (!err) {
                        let formData = Object.assign(that.license, values);
                        var authDate = new Date(that.license.authDateStr);
                        that.license.authDateStr = authDate.getFullYear() + '-' + (authDate.getMonth() + 1) + '-' + authDate.getDate();
                        var stopDate = new Date(that.license.stopDateStr);
                        that.license.stopDateStr = stopDate.getFullYear() + '-' + (stopDate.getMonth() + 1) + '-' + stopDate.getDate();
                        if(!that.isValidateDate(that.license.authDateStr)){
                            message.error("请输入格式为yyyy-MM-dd的授权日期！")
                            return false;
                        }else if(!that.isValidateDate(that.license.stopDateStr)){
                            message.error("请输入格式为yyyy-MM-dd的授权截止日期！")
                            return false;
                        }else if(!that.isValidateNum(that.license.userCountStr)){
                            message.error("授权用户数量应为正整数！")
                            return false;
                        }else if(!that.isValidateMac(that.license.mac)){
                            message.error("MAC地址格式输入有误！")
                            return false;
                        }else if(!that.isValidateIp(that.license.ipAddress)){
                            message.error("IP地址格式输入有误！")
                            return false;
                        }

                        let param={};
                        param.orgName=that.license.orgName;
                        param.softName=that.license.softName;
                        param.authDateStr=that.license.authDateStr;
                        param.stopDateStr=that.license.stopDateStr;
                        param.userCountStr=that.license.userCountStr;
                        param.mac=that.license.mac;
                        param.ipAddress=that.license.ipAddress;


                        //提交到后台
                        saveLicense(param).then((res) => {
                            if(res.result==0){
                                message.error(res.message)
                            }else {
                                message.success(res.message)
                                //调用父组件刷新
                                this.$emit('refresh');
                            }


                        })
                    }
                });

            },
        },
    };
</script>

<style>
    /*固定lable宽度*/
    .ant-descriptions-item-label.ant-descriptions-item-colon{
        width: 200px;
    }

</style>